package com.lcq.demo.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.lcq.demo.common.converter.MusicDTOConverter;
import com.lcq.demo.common.entity.DynamoDbPage;
import com.lcq.demo.common.entity.PageResult;
import com.lcq.demo.common.entity.Result;
import com.lcq.demo.entity.dto.MusicDTO;
import com.lcq.demo.entity.dynamodb.Music;
import com.lcq.demo.service.DynamoDbService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author 240701005
 */
@Tag(name = "DynamoDb模块")
@RestController
@RequestMapping("/api/music")
@Slf4j
//@CrossOrigin(origins = "*", allowedHeaders = "token")
public class MusicController {

    @Resource
    private DynamoDbService dynamoDbService;

    private final static String TABLE_NAME = "test_music";

    /**
     * 查询单个Music
     */
    @Operation(summary = "查询单首歌曲")
    @GetMapping("/{artist}/{songTitle}")
    public Result<Music> getMusicByPk(@PathVariable String artist, @PathVariable String songTitle) {
        try {
            MusicDTO musicDTO = new MusicDTO();
            musicDTO.setArtist(artist);
            musicDTO.setSongTitle(songTitle);
            musicDTO.setTableName(TABLE_NAME);
            if (log.isInfoEnabled()) {
                log.info("MusicController.getMusic.musicDTO：{}", JSON.toJSONString(musicDTO));
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(musicDTO.getArtist()), "作者不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(musicDTO.getSongTitle()), "歌名不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(musicDTO.getTableName()), "tableName不能为空！");
            return Result.ok(dynamoDbService.getItem(musicDTO));
        } catch (Exception e) {
            log.error("MusicController.getMusic.error{}", e.getMessage(), e);
            return Result.fail("查询music失败！");
        }
    }

    /**
     * 根据歌手查找相关音乐
     */
    @Operation(summary = "根据歌手查找歌曲列表")
    @GetMapping("/list/{artist}")
    public Result<DynamoDbPage<Music>> getMusicListByArtist(@PathVariable String artist) {
        try {
            if (log.isInfoEnabled()) {
                log.info("MusicController.getMusicListByArtist.artist：{}", artist);
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(artist), "Artist不能为空！");
            MusicDTO musicDTO = new MusicDTO();
            musicDTO.setArtist(artist);
            musicDTO.setTableName(TABLE_NAME);
            return Result.ok(dynamoDbService.getMusicListByArtist(musicDTO));
        } catch (Exception e) {
            log.error("MusicController.getMusicListByArtist.error{}", e.getMessage(), e);
            return Result.fail("查询music列表失败！");
        }
    }

    /**
     * 插入单个Music
     */
    @Operation(summary = "新增歌曲")
    @PostMapping("/create")
    public Result<Boolean> putMusic(@RequestBody MusicDTO musicDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("MusicController.putMusic.musicDTO：{}", JSON.toJSONString(musicDto));
            }
            musicDto.setTableName(TABLE_NAME);
            Preconditions.checkArgument(!StringUtils.isEmpty(musicDto.getArtist()), "作者不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(musicDto.getSongTitle()), "歌名不能为空！");
            Music music = MusicDTOConverter.INSTANCE.convertToPo(musicDto);
            return Result.ok(dynamoDbService.insertItem(music));
        } catch (Exception e) {
            log.error("MusicController.putMusic.error:{}", e.getMessage(), e);
            return Result.fail("新增music失败！");
        }
    }

    /**
     * 更新Music
     */
    @Operation(summary = "更新歌曲")
    @PostMapping("/update")
    public Result<Boolean> updateMusic(@RequestBody MusicDTO musicDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("MusicController.updateMusic.musicDTO：{}", JSON.toJSONString(musicDTO));
            }
            musicDTO.setTableName(TABLE_NAME);
            Preconditions.checkArgument(!StringUtils.isEmpty(musicDTO.getArtist()), "作者不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(musicDTO.getSongTitle()), "歌名不能为空！");
            return Result.ok(dynamoDbService.update(musicDTO));
        } catch (Exception e) {
            log.error("MusicController.updateMusic.error{}", e.getMessage(), e);
            return Result.fail("更新music失败！");
        }
    }

    /**
     * 删除单个Music
     */
    @Operation(summary = "删除歌曲")
    @DeleteMapping(value = "/{artist}/{songTitle}")
    public Result<Boolean> deleteMusic(@PathVariable String artist, @PathVariable String songTitle) {
        try {
            if (log.isInfoEnabled()) {
                log.info("MusicController.deleteMusic.artist：{}, songTitle:{}", artist, songTitle);
            }
            MusicDTO musicDTO = new MusicDTO();
            musicDTO.setArtist(artist);
            musicDTO.setSongTitle(songTitle);
            return Result.ok(dynamoDbService.delete(MusicDTOConverter.INSTANCE.convertToPo(musicDTO), TABLE_NAME));
        } catch (Exception e) {
            log.error("MusicController.deleteMusic.error{}", e.getMessage(), e);
            return Result.fail("删除music失败！");
        }
    }

    /**
     * 根据索引查找Music
     */
    @Operation(summary = "使用索引查询", description = "根据Awards查询歌曲列表")
    @PostMapping("/index")
    public Result<DynamoDbPage<Music>> getMusicByIndex(@RequestBody MusicDTO musicDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("MusicController.getMusicByIndex.musicDTO：{}", JSON.toJSONString(musicDto));
            }
            musicDto.setTableName(TABLE_NAME);
            return Result.ok(dynamoDbService.getByIndex(musicDto));
        } catch (Exception e) {
            log.error("MusicController.getMusicByIndex.error{}", e.getMessage(), e);
            return Result.fail("查询失败！");
        }
    }

    /**
     * 批量删除
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@RequestBody List<MusicDTO> musicDtoList) {
        try {
            if (log.isInfoEnabled()) {
                log.info("MusicController.batchDelete.musicDtoList:{}", JSON.toJSONString(musicDtoList));
            }
//            List<Music> musicList = MusicDTOConverter.INSTANCE.convetToPoList(musicDtoList);
//            System.out.println(JSON.toJSONString(musicList));
            dynamoDbService.batchDeleteItems(musicDtoList);
            return Result.ok("批量删除成功！");
        } catch (Exception e) {
            log.error("MusicController.batchDelete.error:{}", e.getMessage(), e);
            return Result.fail("批量删除失败！");
        }
    }

    /**
     * 批量新增
     */
    @Operation(summary = "批量新增")
    @PostMapping("/batch")
    public Result<Boolean> batchInsert(@RequestBody List<MusicDTO> musicDtoList) {
        try {
            if (log.isInfoEnabled()) {
                log.info("MusicController.batchInsert.musicDtoList:{}", JSON.toJSONString(musicDtoList));
            }
//            List<Music> musicList = MusicDTOConverter.INSTANCE.convetToPoList(musicDtoList);
//            System.out.println(JSON.toJSONString(musicList));
            dynamoDbService.batchInsertItems(musicDtoList);
            return Result.ok("批量新增成功！");
        } catch (Exception e) {
            log.error("MusicController.batchInsert.error:{}", e.getMessage(), e);
            return Result.fail("批量新增失败！");
        }
    }

}
